package com.web.logindemo.service.permission;

import com.web.logindemo.domain.entity.SysUser;
import com.web.logindemo.service.ISysMenuService;
import com.web.logindemo.service.ISysRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

/**
 * 用户权限管理
 */
@Component
public class SysPermissionService {

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private ISysRoleService roleService;

    /**
     * 获取角色数据权限
     */
    public Set<String> getRolePermission(SysUser user) {
        Set<String> roles = new HashSet<>();
        if(user.isAdmin()){
            roles.add("admin");
        }else{
            roles.addAll(roleService.selectRolePermissionsByUserId(user.getUserId()));
        }
        return roles;
    }

    /**
     * 获取菜单数据权限
     */
    public Set<String> getMenuPermission(SysUser user) {
        Set<String> perms = new HashSet<>();
        // 管理员拥有的权限
        if (user.isAdmin()) {
            perms.add("*:*:*");
        } else {
            perms.addAll(menuService.selectMenuPermsByUserId(user.getUserId()));
        }
        return perms;
    }
}
